﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using MvcContrib;
using MvcContrib.UI.Html;
using System.Text;
using System.Text.RegularExpressions;

namespace CooWork.Services
{
    public static class CustomPager
    {
        public static string cpager(MvcContrib.Pagination.IPagination pagedList,string baseurl,string q)
        {
            StringBuilder sb = new StringBuilder();

            string urlstr = baseurl+="?";


            q = Regex.Replace(q, @"\?", "");
            q = Regex.Replace(q, @"&?page=\d+", "");
            

           

            if (pagedList != null && pagedList.TotalItems > 0)
            {
                sb.AppendLine("<div class=\"w-pagination\"><ul class=\"clearfix\">");
                if (pagedList.PageNumber > 1)
                    sb.Append(@"<li><a href=""" + urlstr + q + "&page=" + (pagedList.PageNumber - 1).ToString() + @""" >前一页</a>");
                else
                    sb.Append("</li>");
                sb.Append(" <li class=\"pageIndex\">");
                sb.AppendLine("");
                for (int i = 1; i <= pagedList.TotalPages; i++)
                {
                    if (i == 1 && pagedList.TotalPages > 6 && pagedList.PageNumber > 6)
                    {
                        sb.Append(@"<a href=""" + urlstr + q + "&page=" + i.ToString() + @""" >" + i.ToString() + "</a><i>...</i>");
                        continue;
                    }

                    if (i == pagedList.TotalPages && pagedList.TotalPages > 6 && pagedList.PageNumber <pagedList.TotalPages-5)
                    {
                        sb.Append(@"<i>...</i><a href=""" + urlstr + q + "&page=" + i.ToString() + @""" >" + i.ToString() + "</a>");
                        continue;
                    }


                    if (pagedList.TotalPages > 6)
                    {
                        if (i < pagedList.PageNumber - 4 && i != 1)
                            continue;
                        if (i > pagedList.PageNumber + 4 && i <pagedList.TotalPages)
                            continue;
                    }

                    if (i == pagedList.PageNumber)
                    {
                        sb.Append(@"<strong>").Append(i.ToString()).Append("</strong>");
                        continue;
                    }                                
                    sb.Append(@"<a href=""" + urlstr + q + "&page=" + i.ToString() + @""" >" + i.ToString() + "</a>");                                                       
                }
                sb.AppendLine("</li><li>");
             
             
                if (pagedList.PageNumber < pagedList.TotalPages)
                    sb.Append(@"<a href=""" + urlstr + q + "&page=" + (pagedList.PageNumber + 1).ToString() + @""" >后一页</a>");
                else
                    sb.Append("");
                sb.AppendLine("</li>");
                sb.AppendLine("</ul></div>");      
            }

            return sb.ToString();
        }

    }
}